package com.jiawa.train.member.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.common.context.LoginMemberContext;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.common.util.SnowUtil;
import com.jiawa.train.member.domain.Passenger;
import com.jiawa.train.member.domain.PassengerExample;
import com.jiawa.train.member.mapper.PassengerMapper;
import com.jiawa.train.member.req.PassengerQueryReq;
import com.jiawa.train.member.req.PassengerSaveReq;
import com.jiawa.train.member.resp.PassengerQueryResp;
import com.jiawa.train.member.service.PassengerService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zzl
 * @version 1.0
 */
@Service
public class PassengerServiceImpl implements PassengerService {
    private static final Logger LOG = LoggerFactory.getLogger(PassengerServiceImpl.class);

    @Resource
    private PassengerMapper passengerMapper;

    /**
     * 新增一条乘客信息
     * @param req
     */
    @Override
    public void save(PassengerSaveReq req) {
        DateTime now = DateTime.now();
        Passenger passenger = BeanUtil.copyProperties(req, Passenger.class);
        passenger.setUpdateTime(now);
        if(ObjectUtil.isNull(passenger.getId())) {
            Long memberId = LoginMemberContext.getId();
            if(isAddByIdCard(memberId, passenger.getIdCard())) {
                passenger.setId(SnowUtil.getSnowflakeNextId());
                passenger.setMemberId(memberId);
                passenger.setCreateTime(now);
                passengerMapper.insert(passenger);
            } else {
                throw new BusinessException(BusinessExceptionEnum.PASSENGER_IS_EXIST);
            }
        } else {
            passengerMapper.updateByPrimaryKeySelective(passenger);
        }
    }

    /**
     * 分页查询会员的乘客信息
     * @param req
     * @return
     */
    @Override
    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq req) {
        PassengerExample passengerExample = new PassengerExample();
        PassengerExample.Criteria criteria = passengerExample.createCriteria();

        if(ObjectUtil.isNotNull(req.getMemberId())) {
            criteria.andMemberIdEqualTo(req.getMemberId());
        }

        LOG.info("查询页码：{}", req.getPage());
        LOG.info("每页条数：{}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Passenger> passengerList = passengerMapper.selectByExample(passengerExample);

        PageInfo<Passenger> pageInfo = new PageInfo<>(passengerList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<PassengerQueryResp> passengerQueryRespList = BeanUtil.copyToList(passengerList, PassengerQueryResp.class);
        PageResp<PassengerQueryResp> pageResp = new PageResp<>();
        pageResp.setList(passengerQueryRespList);
        pageResp.setTotal(pageInfo.getTotal());
        return pageResp;
    }

    /**
     * 根据id删除乘客
     * @param id
     */
    @Override
    public void delete(Long id) {
        passengerMapper.deleteByPrimaryKey(id);
    }

    private boolean isAddByIdCard(Long memberId, String idCard) {
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.createCriteria()
                .andMemberIdEqualTo(memberId)
                .andIdCardEqualTo(idCard);
        List<Passenger> list = passengerMapper.selectByExample(passengerExample);
        if (CollUtil.isEmpty(list)) return true;
        else return false;
    }

    /**
     * 查询我的所有乘客
     */
    public List<PassengerQueryResp> queryMine() {
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.setOrderByClause("name asc");
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        criteria.andMemberIdEqualTo(LoginMemberContext.getId());
        List<Passenger> list = passengerMapper.selectByExample(passengerExample);
        return BeanUtil.copyToList(list, PassengerQueryResp.class);
    }
}
